package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.lzx.model.User;
import com.lzx.model.index.GrainSales;
import com.lzx.model.index.GrainYield;
import com.lzx.model.index.PlantArea;
import com.lzx.model.index.PlantType;
import com.lzx.service.IndexService;
import com.lzx.utils.JsonResultUtil;
import com.lzx.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/sy/index")
public class IndexController {
    @Autowired
    IndexService indexService;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("system/index");
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);
        return mv;
    }
    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
    @RequestMapping("/liuxiangtu")
    public ModelAndView test(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("system/liuxiangtu");
        return mv;
    }
    @ResponseBody
    @RequestMapping("/plantArea")
    public Object plantArea(HttpServletRequest request) {
        List<PlantArea> planeAreaList = null;
        try {
            planeAreaList = indexService.findPlaneArea();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
       /* Object planeAreaListredis = redisService.get("planeAreaList");
        if (planeAreaListredis == null) {
            redisService.set("planeAreaList", JSON.toJSONString(planeAreaList), 120000L);
            System.out.println("tomcat");
            System.out.println(JSON.toJSONString(planeAreaList));
            return JSON.toJSONString(planeAreaList);
        } else {
            System.out.println("redis");
            System.out.println(JSON.toJSONString(planeAreaList));
            return planeAreaListredis;
        }*/
       /* for (PlantArea plantArea : planeAreaList) {
            if(plantArea.getCreatetime() != null){
                SimpleDateFormat sf = new SimpleDateFormat("yyyy");
               *//* System.out.println("Createtime = " + plantArea.getCreatetime());*//*
                plantArea.setYear(sf.format(plantArea.getCreatetime()));
            }
        }*/
        /*return JSON.toJSONString(planeAreaList);*/
        System.out.println("planeAreaList = " + planeAreaList);
        return planeAreaList;
    }
    @ResponseBody
    @RequestMapping("/plantType")
    public Object plantType() {
        List<PlantType> plantType = null;
        double count = 0;
        double percent = 0;
        try {
            plantType = indexService.findPlantType();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        for (PlantType plant : plantType) {
            if(plant.getValue() != null){
                count += Double.parseDouble(plant.getValue());
            }
        }
        for (PlantType plant : plantType) {
            if(plant.getValue() != null){
                double value = Double.parseDouble(plant.getValue());
                percent = value / count * 100;
                String s = percent + "";
                String str = s.substring(0,s.indexOf(".") + 3);
                plant.setPercent(str);
            }
        }
        System.out.println("plantType = " + JSON.toJSONString(plantType));
        /*return JSON.toJSONString(plantType);*/
        return plantType;
    }
    @ResponseBody
    @RequestMapping("/GrainYield")
    public Object GrainYield(HttpServletRequest request) {
        List<GrainYield> GrainYield = null;
        try {
            GrainYield = indexService.findGrainYield();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        System.out.println("GrainYield = "+GrainYield);
        return JSON.toJSONString(GrainYield);
        /*return GrainYield;*/
    }
    @ResponseBody
    @RequestMapping("/GrainSales")
    public Object GrainSales(HttpServletRequest request) {
        List<GrainSales> GrainSales = null;
        try {
            GrainSales = indexService.findGrainSales();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        return JSON.toJSONString(GrainSales);
    }


    @RequestMapping("/indexnew")
    public ModelAndView indexnew(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("zhny/index");
        String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);
        return mv;
    }
}
